<?php
/*
//由于已经可以保证结构正确，所以不从抽象类继承。
require_once("Stratosphere.ServerLog.class.php");
class  StratosphereServerLog extends AbstractStratosphereServerLog{
*/
class StratosphereServerLog {
	
	protected $ServerLogObject=false;
	
	protected $ServerLogPath  ="StratosphereServerLog\\";
	protected $ServerLogString="StratosphereServerLog";
	protected $ServerLogGrpRefresh=100;
	protected $ServerLogGrpMax=10000;
	protected $MemcacheObject =false;
	
	function __construct($options=null) {
		if( !empty($options) ){
			if( is_array($options) ){
				$tmp_str=$options['key'];
				if($tmp_str!='') $this->ServerLogString="StratosphereServerLog-".$tmp_str;
				$tmp_int=$options['limit'];
				if( is_int($tmp_int) && $tmp_int>100) $this->ServerLogGrpMax=$tmp_int * 1 ;
				$tmp_int=$options['group-refresh'];
				if( is_int($tmp_int) && $tmp_int>=1) $this->ServerLogGrpRefresh=$tmp_int * 1 ;
			}
		}
		
		$this->ServerLogObject= new CeFile();
		$this->MemcacheObject = new Memcache;
		$this->MemcacheObject->init();
	}
	function __destruct(){
		if($ServerLogObject){
			unset( $this->ServerLogObject);
		}
		if($MemcacheObject){
			unset( $this->MemcacheObject);
		}
	}

	private function getLogIndexBySettingString($string_key_name){
		$tmp_idx = $this->MemcacheObject->increment($string_key_name);
		if( empty($tmp_idx) ){
			$tmp_str = $this->ServerLogObject->read($this->ServerLogPath . $string_key_name);
			if( empty($tmp_str) ){
				$tmp_idx=0;
				$this->ServerLogObject->write($this->ServerLogPath . $string_key_name,$tmp_idx);
			}else{
				$tmp_idx = $tmp_str - 0;
			}
			$this->MemcacheObject->set($string_key_name,$tmp_idx,0,0);
			return $tmp_idx;
		}else{
			$this->ServerLogObject->write($this->ServerLogPath . $string_key_name,$tmp_idx);
			return $tmp_idx;
		}
	}
	
	private function getGroupIndexBySettingString($string_key_name){
		$tmp_idx = $this->MemcacheObject->get($string_key_name);
		if( empty($tmp_idx) ){
			$tmp_str = $this->ServerLogObject->read($this->ServerLogPath . $string_key_name);
			if( empty($tmp_str) ){
				$tmp_idx=0;
				$this->ServerLogObject->write($this->ServerLogPath . $string_key_name,$tmp_idx);
			}else{
				$tmp_idx = $tmp_str - 0;
			}
			$this->MemcacheObject->set($string_key_name,$tmp_idx,0,0);
			return $tmp_idx;
		}else{
			return $tmp_idx;
		}
	}

	private function incrementGroupIndexBySettingString($string_key_name){
		$tmp_idx = $this->getGroupIndexBySettingString($string_key_name);
		$tmp_idx =$this->MemcacheObject->increment($string_key_name);
		$this->ServerLogObject->write($this->ServerLogPath . $string_key_name,$tmp_idx);
		$tmp_idx = $this->getGroupIndexBySettingString($string_key_name);
		$this->ServerLogObject->write($this->ServerLogPath . $string_key_name,$tmp_idx);
		if( $tmp_idx > $this->ServerLogGrpRefresh ){//清理内存
			$this->MemcacheObject->delete($this->ServerLogString."-".(  $tmp_idx - $this->ServerLogGrpRefresh )."-LogIndexFile.log");
		}
		return $tmp_idx;
	}

	private function log($level,$str){
		//由于ACE完全不支持日志,因此用CeFile代替
		$GroupIndex=$this->getGroupIndexBySettingString($this->ServerLogString."-LogFileGroupIndex.log");//此文件或同名cache保存当前最大的 日志组id号。
		
		$LogIndex=$this->getLogIndexBySettingString($this->ServerLogString."-".$GroupIndex."-LogIndexFile.log");//此文件或同名cache保存当前日志组id号文件对应的最大的 日志文件id号。
		if( $LogIndex > $this->ServerLogGrpMax ){
			$GroupIndex=$this->incrementGroupIndexBySettingString($this->ServerLogString."-LogFileGroupIndex.log");
			$LogIndex=$this->getLogIndexBySettingString($this->ServerLogString."-".$GroupIndex."-LogIndexFile.log");
		}
		
		$LogFile=$this->ServerLogString."-".$GroupIndex."-".$LogIndex.".log";
		
		$access = date("Y/m/d H:i:s");
		$this->ServerLogObject->write($this->ServerLogPath . $LogFile , $level.":".$access ." ".$_SERVER['REMOTE_ADDR']."(".$_SERVER['HTTP_USER_AGENT'].") \n\n".$str, $sync=0);
	}
	
	//打印一条调试日志
	function Debug( $str ){
		$this->log('Debug',$str);
	}
	//打印一条轨迹日志
	function Trace( $str ){
		$this->log('Trace',$str);
		
	}
	//打印一条通知日志
	function Notice( $str ){
		$this->log('Notice',$str);
		
	}
	//打印一条警告日志
	function Warning( $str ){
		$this->log('Warning',$str);
		
	}
	//打印一条致命日志
	function Fatal( $str ){
		$this->log('Fatal',$str);
	}
	//由于ACE完全不支持日志,因此用CeFile代替
	//下面的内容用于查看和删除日志
	
	//获取当前最大的 日志组id号。
	function getMaxLogGroupIndex( ){
		 return $this->ServerLogObject->read($this->ServerLogPath . $this->ServerLogString."-LogFileGroupIndex.log");
	}
	//获取某日志组id号文件对应的最大的 日志文件id号。
	function getMaxLogIndex( $GroupIndex){
		 return $this->ServerLogObject->read($this->ServerLogPath . $this->ServerLogString."-".$GroupIndex."-LogIndexFile.log");
	}
	//获取某日志内容。
	function getLogFile( $GroupIndex,$LogIndex){
		 return $this->ServerLogObject->read($this->ServerLogPath .  $this->ServerLogString."-".$GroupIndex."-".$LogIndex.".log" );
	}
	
	//获取最近的n条日志
	function getLogFiles( $count){
		 $i_cnt=0;
		 $ary_rtn=array();
		 $g_max=$this->getMaxLogGroupIndex();
		 //if( is_int($g_max)){//!empty( $g_max ) && //if( $g_max!='' ){//
			 while($g_max>=0 && $i_cnt< $count){
				$l_max=$this->getMaxLogIndex( $g_max);
				//if( is_int($l_max)){//!empty( $l_max ) && //if( $l_max!='' ){//
					while($l_max>=0 &&  $i_cnt< $count){
						$tmp=$this->getLogFile( $g_max,$l_max);
						if(!empty($tmp)){
							$ary_rtn[$i_cnt++]=array($g_max,$l_max,$tmp);
						}
						$l_max--;
					}
				//}
				$g_max--;
			}
		//}
		return $ary_rtn;
	}
	
	//删除某日志文件
	function deleteLogFileByIndex( $GroupIndex,$LogIndex){
		 return $this->ServerLogObject->delete($this->ServerLogPath . $this->ServerLogString."-".$GroupIndex."-".$LogIndex.".log" );
	}
	//删除某日志组文件
	function deleteGroupFileByIndex( $GroupIndex){
		 return $this->ServerLogObject->delete( $this->ServerLogPath . $this->ServerLogString."-".$GroupIndex."-LogIndexFile.log" );
	}
	//删除某日志组及组内各文件
	function deleteLogGroupByIndex( $GroupIndex){
		$this->MemcacheObject->set($this->ServerLogString."-". $GroupIndex ."-LogIndexFile.log",0);
		$max_log_id=$this->getMaxLogIndex( $GroupIndex);
		//if( !empty( $max_log_id ) && is_int($max_log_id)){
			$max_log_id=$max_log_id * 1 ;
			$tmp_log_id=0;
			while($tmp_log_id<=$max_log_id){
				$this->deleteLogFileByIndex( $GroupIndex,$tmp_log_id++ );
			}
		//}
		$this->deleteGroupFileByIndex( $GroupIndex );
		$this->MemcacheObject->delete($this->ServerLogString."-". $GroupIndex ."-LogIndexFile.log");
	}
	
	//删除全部日志组及组内各文件
	function deleteAllLogByName( $ServerLogString){
		if( $this->ServerLogString=="StratosphereServerLog" || ("StratosphereServerLog-".$ServerLogString) == $this->ServerLogString ){
			$this->MemcacheObject->set($this->ServerLogString."-LogFileGroupIndex.log",0);
			$max_group_id=$this->getMaxLogGroupIndex();
			//if( is_int($max_group_id) ){//if( !empty( $max_group_id ) && is_int($max_group_id)){//
				$max_group_id=$max_group_id * 1 ;
				$tmp_group_id=0;
				while($tmp_group_id<=$max_group_id){
					$this->deleteLogGroupByIndex( $tmp_group_id++);
				}
			//}
			//$this->ServerLogObject->delete($this->ServerLogPath . $this->ServerLogString."-LogFileGroupIndex.log");
			//$this->MemcacheObject->delete($this->ServerLogString."-LogFileGroupIndex.log");
			return true;
		}else{
			return false;
		}
	}
}
?>